//
//  CYCarLoanPublishTVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/17.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCarLoanPublishTVC.h"
#import "MDSlider.h"
#import "CYCompanyUserTool.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+LE.h"
#import "CYCarHelpVC.h"
#import "CYCarRiskRewardTVC.h"
#import "CYJumpsTool.h"
#import "LEMDLib.h"
#import "CYLoanCarM.h"
#import "CYLoanRecordM.h"
#import "MJExtension.h"
#import "CYCreditTool.h"
#import "CYLoginHttpTool.h"

@interface CYCarLoanPublishTVC ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *idCarTF;
@property (weak, nonatomic) IBOutlet UITextField *licencePlateTF;
@property (weak, nonatomic) IBOutlet UITextField *restitutionDataTF;
@property (weak, nonatomic) IBOutlet MDSlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *selectedMoneyLabel;

@property(nonatomic, strong) NSDate *restitutionDate;//到期日期

@property(nonatomic, strong) CYLoanCarM *loanCar;
@property(nonatomic, strong) CYLoanRecordM *loanRecord;
@end

@implementation CYCarLoanPublishTVC

- (CYLoanCarM *)loanCar {
    if (!_loanCar) {
        _loanCar = [CYLoanCarM new];
    }
    return _loanCar;
}

- (CYLoanRecordM *)loanRecord {
    if (!_loanRecord) {
        _loanRecord = [CYLoanRecordM new];
    }
    return _loanRecord;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *submit = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submit:)];
    self.navigationItem.rightBarButtonItem = submit;
    [self initData];
}

- (void)initData {
    self.slider.enabled = NO;
    [self loadReward];
}

- (void)loadReward {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        NSDictionary *param = @{@"userId": user.userId, @"companyId": user.userId, @"token": user.token};
        [CYDiscoveryHttpTool getCompanyCarRewardWithParam:param success:^(NSNumber *account, NSNumber *riskAccount) {
            CGFloat step = 5000;
            if (account.doubleValue < step) {
                [MBProgressHUD showError:@"账号余额不足，请充值后再操作"];
            } else {
//                self.balance = account.integerValue;
                //silder
                self.slider.enabled = YES;
                self.slider.step = step;
                self.slider.minimumValue = 0;
                self.slider.maximumValue = account.integerValue;
            }
        } failure:^(NSString *message, BOOL isLogin) {
            if (!isLogin) {
                [CYCompanyUserTool logoutWithController:self];
                [self.navigationController popToRootViewControllerAnimated:NO];
            }
            [MBProgressHUD showError:message];
        }];
    } else {
        [MBProgressHUD showError:@"请登陆后再操作"];
        return;
    }
}

- (void)submit:(UIBarButtonItem *)bar {
    [self.view endEditing:YES];
    if ([self setupParam]) {
        CYUser *user = [CYLoginHttpTool userData].user;
        NSMutableDictionary *param = self.loanCar.keyValues;
        [param addEntriesFromDictionary:self.loanRecord.keyValues];
        param[@"userId"] = user.userId;
        param[@"token"] = user.token;
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool saveLoanWithParam:param success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showSuccess:message];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popViewControllerAnimated:YES];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    }
}

- (IBAction)restitutionDataBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    [LEMDLib showDatePickerWithDateFormatter:@"yyyy-MM-dd" completion:^(NSString *dateStr, NSDate *date) {
        self.restitutionDataTF.text = dateStr;
        self.restitutionDate = date;
    }];
}
- (IBAction)slider:(MDSlider *)sender {
    NSInteger value = sender.value;
    self.selectedMoneyLabel.text = CYStringFormat(@"￥%ld", (long)value);
}
- (IBAction)helpBtn:(id)sender {
    CYCarRiskRewardTVC *risk = [CYCarRiskRewardTVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    [self.navigationController pushViewController:risk animated:YES];
}

- (BOOL)setupParam {
    if (self.nameTF.text && !self.nameTF.text.isEmpty) {
        self.loanRecord.name = self.nameTF.text;
    } else {
        [MBProgressHUD showError:@"请输入贷款人姓名"];
        return NO;
    }
    if (self.phoneTF.text && !self.phoneTF.text.isEmpty) {
        self.loanRecord.phone = self.phoneTF.text;
    } else {
        [MBProgressHUD showError:@"请输入贷款人手机号"];
        return NO;
    }
    if (self.idCarTF.text && !self.idCarTF.text.isEmpty) {
        self.loanRecord.certificateNumber = self.idCarTF.text;
    } else {
        [MBProgressHUD showError:@"请输入贷款人身份证"];
        return NO;
    }
    if (self.licencePlateTF.text && !self.licencePlateTF.text.isEmpty) {
        self.loanCar.carNumber = self.licencePlateTF.text;
    } else {
        [MBProgressHUD showError:@"请输入抵押车牌号"];
        return NO;
    }
    if (self.restitutionDate) {
        NSString *dateStr = [CYCreditTool timeWithDate:self.restitutionDate formatter:@"yyyy-MM-dd HH:mm:ss"];
        self.loanCar.limitTime = dateStr;
    } else {
        [MBProgressHUD showError:@"请选择到期日期"];
        return NO;
    }
    return YES;
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .1f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}

@end
